<?php

class kkfashionController extends stadmincontroller {

    public function __construct($config) {
        parent::__construct($config);
    }

    protected function addSubMenu() {
        parent::addSubMenu();
        add_submenu_page($this->config ['theme_name'], 'SlopeTracker Menus', 'Theme Menus', 'edit_theme_options', 'nav-menus.php');
    }

    public function loadCss() { // load more css custom here
        echo '<link type="text/css" rel="stylesheet" href="' . $this->config['cssUrl'] . 'prettyphoto/prettyPhoto.css">';
    }

    public function loadScript() { // load more js custom here
        echo '<script type="text/javascript" src="' . $this->config ['scriptUrl'] . 'jquery.prettyPhoto.js"></script>';
        echo '<script type="text/javascript" src="' . $this->config ['scriptUrl'] . 'tiny_mce/tiny_mce.js"></script>';
    }

    public function activationTheme() {
        
    }

    public function index() {

        $option = $this->option;
        //initialize variable to view
        $this->view->option = $option;
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'General Setting';

        if ($_POST['data']) {
            $data = $_POST['data'];
            $data['footer_address'] = str_replace(array("\\'",'\\"'),array("'",'"'),$data['footer_address']);
            $data['footer_showroom'] = str_replace(array("\\'",'\\"'),array("'",'"'),$data['footer_showroom']);

            if ($this->updateOption($data)) {
                $arg = array(
                    'status' => 'success',
                    'message' => __('Update option successful', $config['theme_name'])
                );
                echo json_encode($arg);
                die;
            } else {
                $arg = array(
                    'status' => 'error',
                    'message' => __('Update option unsuccessful', $config['theme_name'])
                );
                echo json_encode($arg);
                die;
            }
        }
    }

    public function tags() {

        $option = $this->option;
        //initialize variable to view
        $this->view->option = $option;
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'Video realated settings';

        if ($_POST['data']) {
            $data = $_POST['data'];

            if ($this->updateOption($data)) {
                $arg = array(
                    'status' => 'success',
                    'message' => __('Update option successful', $config['theme_name'])
                );
                echo json_encode($arg);
                die;
            } else {
                $arg = array(
                    'status' => 'error',
                    'message' => __('Update option unsuccessful', $config['theme_name'])
                );
                echo json_encode($arg);
                die;
            }
        }
    }

    public function footer() {

        $option = $this->option;
        //initialize variable to view
        $this->view->option = $option;
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'Footer Settings';

        if ($_POST['data']) {
            $data = $_POST['data'];
            if ($this->updateOption($data)) {
                $arg = array(
                    'status' => 'success',
                    'message' => __('Update option successful', $config['theme_name'])
                );
                echo json_encode($arg);
                die;
            } else {
                $arg = array(
                    'status' => 'error',
                    'message' => __('Update option unsuccessful', $config['theme_name'])
                );
                echo json_encode($arg);
                die;
            }
        }
    }

    public function home() {

        $option = $this->option;
        //initialize variable to view
        $this->view->option = $option;
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'Home Settings';

        if ($_POST['data']) {
            $data = $_POST['data'];
              $upload_data = wp_upload_dir(); 
            $data['image'] = $upload_data['url'] . '/' . basename($data['image']);
            if ($this->updateOption($data)) {
                $arg = array(
                    'status' => 'success',
                    'message' => __('Update option successful', $config['theme_name'])
                );
                echo json_encode($arg);
                die;
            } else {
                $arg = array(
                    'status' => 'error',
                    'message' => __('Update option unsuccessful', $config['theme_name'])
                );
                echo json_encode($arg);
                die;
            }
        }
    }

    public function service() {

        $option = $this->option;
        //initialize variable to view
        $this->view->option = $option;
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'Service Settings';

        if ($_POST['data']) {
            $data = $_POST['data'];
              $upload_data = wp_upload_dir();
            $data['image_service_1'] = $upload_data['url'] . '/' . basename($data['image_service_1']);
            $data['image_service_2'] = $upload_data['url'] . '/' . basename($data['image_service_2']);
            $data['image_service_3'] = $upload_data['url'] . '/' . basename($data['image_service_3']);
            $data['image_service_4'] = $upload_data['url'] . '/' . basename($data['image_service_4']);
            $data['image_service_5'] = $upload_data['url'] . '/' . basename($data['image_service_5']);
            if ($this->updateOption($data)) {
                $arg = array(
                    'status' => 'success',
                    'message' => __('Update option successful', $config['theme_name'])
                );
                echo json_encode($arg);
                die;
            } else {
                $arg = array(
                    'status' => 'error',
                    'message' => __('Update option unsuccessful', $config['theme_name'])
                );
                echo json_encode($arg);
                die;
            }
        }
    }

     public function contact() {

        $option = $this->option;
        //initialize variable to view
        $this->view->option = $option;
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'Contact Settings';

        if ($_POST['data']) {
            $data = $_POST['data'];
            $data['time_contact'] = str_replace(array("\\'",'\\"'),array("'",'"'),$data['time_contact']);
            $data['branch_contact'] = str_replace(array("\\'",'\\"'),array("'",'"'),$data['branch_contact']);
            if ($this->updateOption($data)) {
                $arg = array(
                    'status' => 'success',
                    'message' => __('Update option successful', $config['theme_name'])
                );
                echo json_encode($arg);
                die;
            } else {
                $arg = array(
                    'status' => 'error',
                    'message' => __('Update option unsuccessful', $config['theme_name'])
                );
                echo json_encode($arg);
                die;
            }
        }
    }

    public function sitemap() {

        $option = $this->option;
        //initialize variable to view
        $this->view->option = $option;
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'Sitemap Settings';

        if ($_POST['data']) {
            $data = $_POST['data'];
            if ($this->updateOption($data)) {
                $arg = array(
                    'status' => 'success',
                    'message' => __('Update option successful', $config['theme_name'])
                );
                echo json_encode($arg);
                die;
            } else {
                $arg = array(
                    'status' => 'error',
                    'message' => __('Update option unsuccessful', $config['theme_name'])
                );
                echo json_encode($arg);
                die;
            }
        }
    }

    public function useremail() {
        $useremail_model = $this->loadmodel("callme");

        // short action handle
        if ($_REQUEST['short_action'] == 'delete') {

            if ($useremail_model->deleteRecords($_REQUEST['id'])) {
                $arg = array(
                    'status' => 'success',
                    'message' => __('Delete successful', $config['plugin_name'])
                );
                echo $this->functions->sendMessage(json_encode($arg));
            } else {
                $arg = array(
                    'status' => 'error',
                    'message' => __('Delete unsuccessful', $config['plugin_name'])
                );
                echo $this->functions->sendMessage(json_encode($arg));
            }
        }

        $args = array();

        $cur_page = $_REQUEST['cur_page'] ? $_REQUEST['cur_page'] : 1;
        $limit = $this->config['per_page'];
        $start = ($cur_page - 1) * $limit;

        $sort = $_REQUEST['sort'] ? $_REQUEST['sort'] : 0;
        $sort_type = $_REQUEST['sort_type'] ? $_REQUEST['sort_type'] : 0;
        if ($_REQUEST['sort']) {
            $args['order'] = array($sort, $sort_type);
        } else {
            $args['order'] = array('id', 'desc');
        }
        $args['limit'] = array($start, $limit);

        $results = $useremail_model->getRecords($args);
        $args['limit'] = array();
        $total = $useremail_model->countRecords($args);

        $pagination = $this->functions->pagination($start, $limit, $total, $cur_page, $this->actionName);

        $columns = array(
            'id' => __('ID', $this->config['plugin_name']),
            'value' => __('Value', $this->config['plugin_name']),
            'adddate' => __('Date/Time', $this->config['plugin_name']),
        );
        register_column_headers('stplugin_stplugin', $columns);

        //initialize variable to view
        $this->view->results = $results;
        $this->view->pagination = $pagination;
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'Ring meg';
    }

    public function gallery() {

        $gallery = $this->loadmodel("st_frosozoo_gallery");

        // short action handle
        if ($_REQUEST['short_action'] == 'delete') {

            if ($gallery->deleteRecords($_REQUEST['id'])) {
                $arg = array(
                    'status' => 'success',
                    'message' => __('Delete successful', $config['plugin_name'])
                );
                echo $this->functions->sendMessage(json_encode($arg));
            } else {
                $arg = array(
                    'status' => 'error',
                    'message' => __('Delete unsuccessful', $config['plugin_name'])
                );
                echo $this->functions->sendMessage(json_encode($arg));
            }
        }

        $args = array();

        $cur_page = $_REQUEST['cur_page'] ? $_REQUEST['cur_page'] : 1;
        $limit = $this->config['per_page'];
        $start = ($cur_page - 1) * $limit;

        $sort = $_REQUEST['sort'] ? $_REQUEST['sort'] : 0;
        $sort_type = $_REQUEST['sort_type'] ? $_REQUEST['sort_type'] : 0;
        if ($_REQUEST['sort']) {
            $args['order'] = array($sort, $sort_type);
        }
        $args['limit'] = array($start, $limit);

        $results = $gallery->getRecords($args);
        $total = $gallery->countRecords();

        $pagination = $this->functions->pagination($start, $limit, $total, $cur_page, $this->actionName);

        $columns = array(
            'id' => __('ID', $this->config['plugin_name']),
            'image' => __('Photo', $this->config['plugin_name']),
            'add_date' => __('Add Date', $this->config['plugin_name']),
            'photo_url' => __('URL', $this->config['plugin_name'])
        );
        register_column_headers('sttheme_sttheme', $columns);

        //initialize variable to view
        $this->view->results = $results;
        $this->view->pagination = $pagination;
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'Gallery lists';
        $this->view->iconNew = 'Create new photo';
        $this->view->viewNew = 'galleryEdit';
    }

    public function galleryEdit() {

        $gallery = $this->loadmodel("st_frosozoo_gallery");
        $results = '';
        if (isset($_REQUEST['id'])) {
            $id = $_REQUEST['id'];
            $args = array();
            $args['where'] = " `id` = $id";
            $results = $gallery->getRecords($args);
        }
        $result = $results[0];
        //initialize variable to view
        $this->view->result = $result;
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'New Photo';
        $this->view->headTitle2 = 'Edit Photo';

        // Handle post
        if ($_POST['data']) {
            echo $gallery->saveGallery($_POST['data']);
        }
    }

    public function addPrice() {

        global $post;
//        $this->view->the_texts      = unserialize( get_post_meta($post->ID, 'the_text', true) );
//        $this->view->the_links      = unserialize( get_post_meta($post->ID, 'the_link', true) );
//        $this->view->the_names      = unserialize( get_post_meta($post->ID, 'the_name', true) );
//        $this->view->album_ids      = unserialize( get_post_meta($post->ID, 'album_id', true) );
//        $this->view->image_products = get_post_meta($post->ID, 'image_product');
//        $this->view->content_types  = unserialize( get_post_meta($post->ID, 'content_type', true) );
//        $this->view->pdf_products   = get_post_meta($post->ID, 'pdf_product');

        $this->view->pageID = $post->ID;
    }

}

?>